define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var selectedValue = [];
    var getPageParams = [];
    var getSchoolNames = [];
    var deledValue = [];
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'classes/school/index',
                    add_url: 'classes/school/add',
                    edit_url: 'classes/school/edit',
                    del_url: 'classes/school/del',
                    multi_url: 'classes/school/multi',
                    dragsort_url: '',
                    table: 'classes_school',
                }
            });
            function getParamByUrl(name) { //从url中获取参数
                let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
                let r = decodeURI(decodeURI(window.location.search)).substr(1).match(reg);
                if (r != null) {
                    return unescape(r[2]);
                };
                return '';
            }
            getPageParams = getParamByUrl('schoolIds').split(',');
            getPageParams = getPageParams.map((item) => {
                return parseInt(item);
            })
            getSchoolNames = getParamByUrl('schoolName').split(',');
            getSchoolNames = getSchoolNames.map((item) => {
                return item;
            })

            $.fn.bootstrapTable.locales[Table.defaults.locale]['formatSearch'] = function(){return "学校名称";}
            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortOrder: 'asc',
                maintainSelected:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'username', sortable: true, title: __('账号')},
                        {field: 'nickname', sortable: true, title: __('School_name')},
                        {field: 'province', sortable: true, title: __('省份')},
                        {field: 'city', sortable: true, title: __('市')},
                        {field: 'district', sortable: true, title: __('区')},
                        {field: 'contact_name', sortable: true, title: __('Contact_name')},
                        {field: 'mobile', sortable: true, title: __('Contact_mobile')},
                        // {field: 'tiku.name', sortable: true, title: __('题库设置')},
                        // {field: 'contact_role', sortable: true, title: __('Contact_role')},
                        /*{field: 'classes_review', sortable: true, title: __('Classes_review'),searchList: {"0": __('不审核'), "1": __('审核')},formatter: function(value, row, index){
                            return value == 0 ? '不审核' : '审核'
                        }},*/
                        /*{
                            field: 'buttons',title: __('Admin'), table: table,
                            buttons: [{
                                name: 'admin',
                                text: __('Admin'),
                                classname: 'btn btn-xs btn-success btn-dialog',
                                title: function (row) { return row.school_name + '管理员'; },
                                icon: 'fa fa-eye',
                                url: 'classes/school/admin?school_id={ids}'
                            }],
                            formatter: Table.api.formatter.buttons
                        },*/
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
                search: true,
                commonSearch: false,
                //点击全选框时触发的操作        
                onCheckAll:function(row){ 
                    Controller.api.bindevent();
                    var checkedbox= $("#table").bootstrapTable('getSelections'); 	//获取选中的数据
                    //如果全局变量中没有数据，直接赋值
                    if (!selectedValue) {
                        selectedValue = checkedbox;
                        return ;
                    }
                    var c = [];
                    //取两个数组的差集
                    if (selectedValue.length > checkedbox.length) {
                        c = selectedValue.filter(function(v) { return checkedbox.indexOf(v) == -1})
                    } else {
                        c = checkedbox.filter(function(v) { return selectedValue.indexOf(v) == -1})
                    }
                    //将数据添加到全局选中变量数据中
                    $.each(c, function(index,value){
                        selectedValue.push(value);
                    })
                },
                //取消所有
                onUncheckAll: function (row) {
                    Controller.api.bindevent();
                    var checkedbox= $("#table").bootstrapTable('getSelections'); 
                    var c = null;
                    //取两个数组的差集
                    if (selectedValue.length > checkedbox.length) {
                        c = selectedValue.filter(function(v) { return checkedbox.indexOf(v) == -1})
                    } else {
                        c = checkedbox.filter(function(v) { return selectedValue.indexOf(v) == -1})
                    }
                    var diffKey = null;
                    $.each(c, function(index,value){
                        //获取移除数据在全局选中变量数据中所在位置，并删除
                        diffKey = selectedValue.indexOf(value);
                        selectedValue.splice(diffKey, 1);
                        //如果记录删除选中不存在则放入
                        if (deledValue.indexOf(value)==-1) {
                            deledValue.push(value);
                        }
                    })


                },
                //点击行前面的选择框
                onCheck:function(row,$element){
                    Controller.api.bindevent();
                    var checkedbox= $("#table").bootstrapTable('getSelections'); 
                    //如果全局变量中没有数据，直接赋值
                    if (selectedValue.length == 0) {
                        selectedValue = checkedbox;
                        return ;
                    }
                    var c = [];
                    //取两个数组的差集
                    if (selectedValue.length > checkedbox.length) {
                        c = selectedValue.filter(function(v) { return checkedbox.indexOf(v) == -1})
                    } else {
                        c = checkedbox.filter(function(v) { return selectedValue.indexOf(v) == -1})
                    }
                    //将数据添加到全局选中变量数据中
                    $.each(c, function(index,value){
                        selectedValue.push(value);
                    })
                },
                //取消选中
                onUncheck:function(row,$element){
                    Controller.api.bindevent();
                    var checkedbox= $("#table").bootstrapTable('getSelections');  
                    //取两个数组的差集
                    if (selectedValue.length > checkedbox.length) {
                        c = selectedValue.filter(function(v) { return checkedbox.indexOf(v) == -1})
                    } else {
                        c = checkedbox.filter(function(v) { return selectedValue.indexOf(v) == -1})
                    }
                    var diffKey = null;
                    $.each(c, function(index,value){
                        //获取移除数据在全局选中变量数据中所在位置，并删除
                        diffKey = selectedValue.indexOf(value);
                        selectedValue.splice(diffKey, 1);
                        //如果记录删除选中不存在则放入
                        if (deledValue.indexOf(value)==-1) {
                            deledValue.push(value);
                        }
                    })
                },
                onPostBody:function() {
                    //防止出现无效数据的情况
                    if (getPageParams.length>0 && parseInt(getPageParams[0]) > 0) {
                        $('#table').bootstrapTable("checkBy", {field:"id", values: getPageParams})
                    } else {
                        getPageParams = [];
                        getSchoolNames = [];
                    }
                        
                }
                
                
            });
            // 为表格绑定事件
            Table.api.bindevent(table);

        },
        admin: function () {
            // 为表格绑定事件
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                $(document).on('click', '.btn-success', function(){
                    //如果有之前传入值
                    if (getPageParams.length > 0) {
                        var oldIndex = '';
                        $.each(deledValue, function(index, value) {
                            oldIndex = getPageParams.indexOf(value.id);
                            //判断之前传入值是否有删除的，有删除的去掉
                            if (oldIndex>-1) {
                                getPageParams.splice(oldIndex);
                                getSchoolNames.splice(oldIndex);
                            }
                        })
                    }


                    $.each(selectedValue, function(key, val) {
                        //加入历史数据不存在的数据
                        if (getPageParams.indexOf(val.id) == -1) {
                            getPageParams.push(val.id);
                            getSchoolNames.push(val.nickname)
                        }
                    })
                    //返回数据
                    Fast.api.close({ids:getPageParams, getSchoolNames:getSchoolNames});
                
                });
            },
            
        }
    };
    
    return Controller;
});